package com.san.oms.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.san.base.controller.BaseController;
import com.san.common.annotation.Log;
import com.san.common.utils.PageUtils;
import com.san.common.utils.Query;
import com.san.common.utils.R;
import com.san.oms.domain.ScoreLevelDO;
import com.san.oms.service.ScoreLevelService;
import com.san.oms.vo.ScoreLevelVO;

@RequestMapping("/ce/scorelevel")
@Controller
public class ScoreLevelController extends BaseController{

	private String PREFIX = "ce/scorelevel";
	private static final Logger logger = LoggerFactory.getLogger(ScoreLevelController.class);
	@Autowired
	private ScoreLevelService scoreLevelService;
	
	@GetMapping("")
	@RequiresPermissions("ce:scorelevel:view")
	String source(Model model) {
		return PREFIX + "/view";
	}
	
	/**
	 * 查询评分等级列表
	 * @param params
	 * @return
	 */
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("ce:scorelevel:view")
	public PageUtils list(@RequestParam Map<String, Object> params){
		PageUtils pageUtils = null;
		try{
			// 查询列表数据
			Query query = new Query(params);
			List<ScoreLevelDO> scoreLevelList = scoreLevelService.list(query);
			int total = scoreLevelService.count(query);
			pageUtils = new PageUtils(scoreLevelList, total);
		}catch(Exception e){
			logger.error("获取评分等级列表失败",e);
		}
		return pageUtils;
	}
	
	/**
	 * 查询评分等级名称
	 * @param params
	 * @return
	 */
	@ResponseBody
	@GetMapping("/scoreName")
	public List<ScoreLevelDO> getScoreName() {
		// 查询列表数据
		List<ScoreLevelDO> list = scoreLevelService.getScoreName();
		return null == list ? new ArrayList<ScoreLevelDO>() : list;
	}
	
	/**
	 * 进入新增页面
	 * @return
	 */
	@GetMapping("/add")
	@RequiresPermissions("ce:scorelevel:add")
	String add() {
		return PREFIX + "/add";
	}
	
	/**
	 * 保存新增评分等级
	 * @param code
	 * @return
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("ce:scorelevel:add")
	@Log("新增评分等级")
	public R save(ScoreLevelVO scoreLevel) {
		try{
				int result = scoreLevelService.save(scoreLevel);
				if(result == -1){
					return R.error("该评分等级已经存在");
				}else if (result > 0) {
					return R.ok();
				}
			}catch (Exception e) {
				logger.error("新增评分等级失败",e);
			}
		return R.error();
	}
	
	/**
	 * 编辑评分等级
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping("/edit/{id}")
	@RequiresPermissions("ce:scorelevel:edit")
	@Log("编辑评分等级")
	String edit(@PathVariable("id") Long id, Model model) {
		ScoreLevelDO scoreLevelDO = scoreLevelService.getById(id);
		model.addAttribute("scoreLevelDO", scoreLevelDO);
		return PREFIX + "/edit";
	}
	
	/**
	 * 修改系统编码
	 * @param code
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/doEdit")
	@RequiresPermissions("ce:scorelevel:edit")
	@Log("修改评分等级")
	public R doEdit(ScoreLevelVO scoreLevel) {
		try{
			int result = scoreLevelService.doEdit(scoreLevel);
			if(result == -1){
				return R.error("该评分等级已经存在");
			}else if(result > 0){
				return R.ok();
			}
		}catch(Exception e){
			logger.error("修改评分等级失败",e);
		}
		return R.error();
	}
	
	/**
	 * 删除评分等级
	 * @param id
	 * @return
	 */
	@PostMapping("/remove")
	@ResponseBody
	@RequiresPermissions("ce:scorelevel:remove")
	@Log("删除评分等级")
	public R remove(Long id) {
		try{
			int state = scoreLevelService.remove(id);
			if(state == -1){
				return R.error("该评分等级存在明细，无法删除");
			}else if (state > 0) {
				return R.ok();
			}
		}catch(Exception e){
			logger.error("删除评分等级失败",e);
		}
		return R.error();
	}

	/**
	 * 批量删除评分等级
	 */
	@PostMapping("/batchRemove")
	@ResponseBody
	@RequiresPermissions("ce:scorelevel:batchRemove")
	@Log("批量删除评分等级")
	public R remove(@RequestParam("ids[]") Long[] ids) {
		try{
			int state = scoreLevelService.batchRemove(ids);
			if(state == -1){
				return R.error("所选评分等级中存在有明细项，无法删除");
			}else if (state > 0) {
				return R.ok();
			}
		}catch(Exception e){
			logger.error("批量删除评分等级失败",e);
		}
		return R.error();
	}
	
}
